package se.study.socket;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.*;
import java.net.Socket;

/**
 * @Author: tiger
 * @Date:2018/6/29
 * @Description: 复习socket网络编程
 */
public class SocketClient {
    public static void main(String[] args) throws IOException {
        // 1、创建客户端Socket，指定服务器地址和端口
        Socket client = new Socket("localhost", 6060);
        // 2、获取输出流，向服务器端发送信息
        OutputStream outputStream = client.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        printWriter.write("我是clent!!!");
        printWriter.flush();
        client.shutdownOutput();
        // 3、获取输入流，并读取服务器端的响应信息
        InputStream inputStream = client.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        // 4、输出
        String data = null;
        while ((data = bufferedReader.readLine()) != null) {
            System.out.println(data);
        }
        client.shutdownInput();
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        printWriter.close();
        outputStream.close();
    }
}

